/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BANewsCell.m
//  redia-bapps-ios
//
//  Created by Nikolaj Tørring on 10/16/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BANewsCell.h"
#import "BALibrarySettings.h"
#import "LazyLoadManager.h"
#import "InfoGalleriImageUrlUtils.h"

@interface BANewsCell()

@property (weak, nonatomic) IBOutlet UIView *highlightView;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *arrowImageView;
@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *loadingIndicator;

@property (weak, nonatomic) IBOutlet UIView *primaryTitleContainer;
@property (weak, nonatomic) IBOutlet UIView *secondaryTitleContainer;
@property (weak, nonatomic) IBOutlet UILabel *secondaryTitleLabel;

@end

@implementation BANewsCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)awakeFromNib{
    if (RD_IS_IPAD) {
        BA_LABEL_SET_FONT_REGULAR(self.titleLabel);
        BA_LABEL_SET_FONT_LIGHT(self.secondaryTitleLabel);
    } else {
        BA_LABEL_SET_FONT_LIGHT(self.titleLabel);
    }
    if (self.arrowImageView != nil) {
        UIImage* arrowImage = [[BALibrarySettings instance] getCachedImageNamed:@"white_arrow_right" multiplyWithColor:[BALibrarySettings instance].getCustomerColorNormal];
        [self.arrowImageView setImage:arrowImage];
    }
}

-(void)layoutSubviews{
    if (self.frame.size.height > 200) {
        [self.secondaryTitleContainer setHidden:NO];
        [self.primaryTitleContainer setHidden:YES];
        self.secondaryTitleLabel.textColor = [[BALibrarySettings instance] getCustomerColorDark];
    } else{
        [self.secondaryTitleContainer setHidden:YES];
        [self.primaryTitleContainer setHidden:NO];
    }
    [super layoutSubviews];
}

-(void)setHighlighted:(BOOL)highlighted{
    [self setHighlighted:highlighted animated:NO];
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    [self.highlightView setHidden:!highlighted];
    //[super setHighlighted:highlighted animated:animated];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

-(void)setNews:(BANews *)news{
    if (_news != news) {
        _news = news;
        
        NSString* titlestring = news.headline;
        
        if ([titlestring length]==0) {
            titlestring=@"";
        }
        [self.titleLabel setText:titlestring];
        [self.secondaryTitleLabel setText:[titlestring uppercaseString]];
        
        if (news.imageUrl.length > 0) {
            NSString* resize_url;
            if (RD_IS_IPAD) {
                //We download images larger than needed, as the cells may vary in size!
                resize_url = [InfoGalleriImageUrlUtils getResizedImageUrl:news.imageUrl toWidth:400/*256*/ toHeight:400 usingQuality:7 withMode:IMAGE_URL_UTILS_RESIZE_MODE_BOUNDING_BOX outputFormat:@"jpeg" doubleUpForRetina:YES];
            } else{
                CGSize imageSize = self.coverImageView.frame.size;
                resize_url = [InfoGalleriImageUrlUtils getResizedImageUrl:news.imageUrl toWidth:imageSize.width toHeight:imageSize.height usingQuality:7 withMode:IMAGE_URL_UTILS_RESIZE_MODE_CROP_EDGES outputFormat:@"jpeg" doubleUpForRetina:YES];
            }
            
            [[LazyLoadManager instance] lazyLoadUIImageFromUrl:resize_url withOwner:self setScaleforRetina:YES onSuccess:^(UIImage *image) {
                [self.coverImageView setImage:image];
                [UIView animateWithDuration:0.25 animations:^{
                    [self.coverImageView setAlpha:1];
                } completion:^(BOOL finished) {
                    [self.loadingIndicator stopAnimating];
                }];
            } onError:^(LazyLoaderErrorCode errorCode, NSString *errorMessage) {
                
            }];
        } else{
            [self.loadingIndicator stopAnimating];
        }
        
    }
}

@end
